package net.zdsoft.wpchat.support.action.system;

import java.util.List;

import javax.annotation.Resource;

import net.zdsoft.keel.util.StringUtils;
import net.zdsoft.keel.util.UUIDUtils;
import net.zdsoft.keel.util.Validators;
import net.zdsoft.wpchat.android.client.BasicAction;
import net.zdsoft.wpchat.support.entity.SysOption;
import net.zdsoft.wpchat.support.service.SysOptionService;

/**
 * 系统参数配置模块Action
 * 
 * @author xuan
 * @version $Revision: 1.0 $, $Date: 2014-9-21 下午12:40:43 $
 */
public class OptionAction extends BasicAction {
    private static final long serialVersionUID = -3615887325201868008L;

    @Resource
    private SysOptionService sysOptionService;

    private List<SysOption> optionList;

    private SysOption sysOption;
    private String id;
    private String name;
    private String value;
    private String description;

    private String inputStream;

    /**
     * 获取系统参数
     * 
     * @return
     */
    public String optionList() {
        optionList = sysOptionService.getAllSysOption();
        return SUCCESS;
    }

    /**
     * 修改系统参数
     * 
     * @return
     */
    public String modifyOption() {
        if (Validators.isEmpty(name)) {
            inputStream = getError("请输入名称");
            return SUCCESS;
        }
        if (StringUtils.getRealLength(name) > 96) {
            inputStream = getError("请输入名称不能超过96个字符，1个汉字为2个字符");
            return SUCCESS;
        }
        if (Validators.isEmpty(value)) {
            inputStream = getError("请输入名称对应的值");
            return SUCCESS;
        }
        if (StringUtils.getRealLength(value) > 384) {
            inputStream = getError("请输入名称对应的值不能超过384个字符，1个汉字为2个字符");
            return SUCCESS;
        }
        if (StringUtils.getRealLength(description) > 384) {
            inputStream = getError("请输入描述不能超过384个字符，1个汉字为2个字符");
            return SUCCESS;
        }

        try {
            SysOption sysOption = new SysOption();
            sysOption.setId(id);
            sysOption.setName(name);
            sysOption.setValue(value);
            sysOption.setDecription(description);
            sysOptionService.modifySysOption(sysOption);

            inputStream = getMessage("更新系统配置成功！");
        }
        catch (Exception e) {
            inputStream = getError(e.getMessage());
        }

        return SUCCESS;
    }

    /**
     * 删除系统参数
     * 
     * @param id
     * @return
     */
    public String removeOption() {
        sysOptionService.removeOption(id);
        inputStream = getMessage("删除成功！");
        return SUCCESS;
    }

    /**
     * 添加系统参数
     * 
     * @param sysOption
     * @return
     */
    public String addOption() {
        if (Validators.isEmpty(name)) {
            inputStream = getError("请输入名称");
            return SUCCESS;
        }
        if (StringUtils.getRealLength(name) > 96) {
            inputStream = getError("请输入名称不能超过96个字符，1个汉字为2个字符");
            return SUCCESS;
        }
        if (Validators.isEmpty(value)) {
            inputStream = getError("请输入名称对应的值");
            return SUCCESS;
        }
        if (StringUtils.getRealLength(value) > 384) {
            inputStream = getError("请输入名称对应的值不能超过384个字符，1个汉字为2个字符");
            return SUCCESS;
        }
        if (StringUtils.getRealLength(description) > 384) {
            inputStream = getError("请输入描述不能超过384个字符，1个汉字为2个字符");
            return SUCCESS;
        }

        SysOption sysOption = new SysOption();
        sysOption.setId(UUIDUtils.newId());
        sysOption.setName(name);
        sysOption.setValue(value);
        sysOption.setDecription(description);
        sysOptionService.addOption(sysOption);

        inputStream = getMessage("操作成功");
        return SUCCESS;
    }

    public List<SysOption> getOptionList() {
        return optionList;
    }

    public void setOptionList(List<SysOption> optionList) {
        this.optionList = optionList;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getValue() {
        return value;
    }

    public void setValue(String value) {
        this.value = value;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public String getInputStream() {
        return inputStream;
    }

    public void setInputStream(String inputStream) {
        this.inputStream = inputStream;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public SysOption getSysOption() {
        return sysOption;
    }

    public void setSysOption(SysOption sysOption) {
        this.sysOption = sysOption;
    }

}
